/**
 * 
 */
package com.wistech.search.solr.extend.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.wistech.commons.dao.hibernate.HibernateDao;
import com.wistech.search.solr.extend.model.Product;

/**
 * @author Liuye
 * 
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository
public class ProductDao extends HibernateDao<Product, Long> {
	private static final String QUERY_SCORE = "SELECT score FROM product WHERE id=?";
	private static final String QUERY_ALL_SCORE = "SELECT id,score FROM product";
	private static final String QUERY_ALL_ORDER_NUM = "SELECT id,order_num FROM product WHERE order_num>0 ORDER BY order_num DESC";

	public Float getScore(final Long id) {
		return (Float) getHibernateTemplate().executeWithNativeSession(
				new HibernateCallback() {
					public Object doInHibernate(final Session session)
							throws HibernateException {
						final Query q = session.createSQLQuery(QUERY_SCORE)
								.addScalar("score", StandardBasicTypes.FLOAT);
						q.setParameter(0, id);
						q.setMaxResults(1);
						List<Float> result = q.list();
						if (result.size() > 0) {
							return result.get(0);
						}
						return 0f;
					}
				});
	}

	public Map<Long, Float> getAllScores() {
		List<Map<String, Object>> scoreList = (List<Map<String, Object>>) getHibernateTemplate()
				.executeWithNativeSession(new HibernateCallback() {
					public Object doInHibernate(final Session session)
							throws HibernateException {
						final Query q = session.createSQLQuery(QUERY_ALL_SCORE)
								.addScalar("id", StandardBasicTypes.LONG)
								.addScalar("score", StandardBasicTypes.FLOAT);
						q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
						return q.list();
					}
				});
		Map<Long, Float> scores = new HashMap<Long, Float>();
		for (Map<String, Object> score : scoreList) {
			scores.put((Long) score.get("id"), (Float) score.get("score"));
		}
		return scores;
	}

	public Map<Long, Float> findOrderNums() {
		List<Map<String, Object>> orderNumMapList = (List<Map<String, Object>>) getHibernateTemplate()
				.executeWithNativeSession(new HibernateCallback() {
					public Object doInHibernate(final Session session)
							throws HibernateException {
						final Query q = session
								.createSQLQuery(QUERY_ALL_ORDER_NUM)
								.addScalar("id", StandardBasicTypes.LONG)
								.addScalar("order_num",
										StandardBasicTypes.FLOAT);
						q.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
						return q.list();
					}
				});
		Map<Long, Float> orderNums = null;
		if (orderNumMapList != null && !orderNumMapList.isEmpty()) {
			orderNums = new HashMap<Long, Float>(orderNumMapList.size());
			for (Map<String, Object> idAndOrderNum : orderNumMapList) {
				orderNums.put((Long) idAndOrderNum.get("id"),
						(Float) idAndOrderNum.get("order_num"));
			}
		} else {
			orderNums = new HashMap<Long, Float>(1);
		}
		return orderNums;
	}
}
